package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.Ads;
import ex.com.jm.food.service.AdminAdsService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;

public class AdminAdsAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private AdminAdsService adminAdsService;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String order;
	private String sort;
	private Integer status;
	private String adId;
	private String s_date;
	private String e_date;
	private Integer type;
	private String adName;
	private Ads ad;
	private Map<String, String> dateMap;
	public Ads getAd() {
		return ad;
	}
	public void setAd(Ads ad) {
		this.ad = ad;
	}
	public AdminAdsService getAdminAdsService() {
		return adminAdsService;
	}
	public void setAdminAdsService(AdminAdsService adminAdsService) {
		this.adminAdsService = adminAdsService;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	public String getAdId() {
		return adId;
	}
	public void setAdId(String adId) {
		this.adId = adId;
	}
	
	public String getS_date() {
		return s_date;
	}
	public void setS_date(String s_date) {
		this.s_date = s_date;
	}
	public String getE_date() {
		return e_date;
	}
	public void setE_date(String e_date) {
		this.e_date = e_date;
	}
	
	public Integer getType() {
		return type;
	}
	public void setType(Integer type) {
		this.type = type;
	}
	public Map<String, String> getDateMap() {
		return dateMap;
	}
	public void setDateMap(Map<String, String> dateMap) {
		this.dateMap = dateMap;
	}
	public String getAdName() {
		return adName;
	}
	public void setAdName(String adName) {
		this.adName = adName;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	
	
	public String adsList(){
		dateMap=DateUtils.getDate();
		return "adsList";
	}
	
	public String editAds(){
		ad=this.getAdminAdsService().editAds(adId);
		return "edit";
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void getAds(){
		System.out.println(s_date+"===>>"+e_date);
	   HttpServletResponse response = ServletActionContext.getResponse();  
	   response.setContentType("text/plain;charset=utf-8");  
		int offset=(initPage-1)*pageRow;
		List<Ads> ads=this.getAdminAdsService().getAds(offset, pageRow,adName,s_date, e_date, type, status, sort, order);
	    totalCount=this.getAdminAdsService().adsCount(adName,s_date, e_date, type, status);
	    List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
	    for (int i = 0; i < ads.size(); i++) {
			Map map=(Map) ads.get(i);
			list.add(map);
		}
	    JSONArray jsonAds=JSONArray.fromObject(list);
	    JSONObject jo=JsonUtils.buildJson(jsonAds, initPage, pageRow, totalCount);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jo);
		out.print(jo);
		out.flush();
		out.close();
	}

	public String updateStatus(){
		boolean flag=this.getAdminAdsService().updateStatus(status, adId);
		if(flag==true){
			return "adsList";
		}
		return "updateFail";
		
	}
}
